//
//  LTChangeClassInfoVC.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "LTChangeClassInfoVC.h"
#import <Photos/Photos.h>
#import "LTClassDetailModel.h"
#import "LTClassAPIManager.h"
@interface LTChangeClassInfoVC ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIButton *saveButton;

@property(nonatomic ,strong)UITextField *gradeNameTextfield;

@property(nonatomic ,strong)NSArray *titleArray;

@property(nonatomic ,strong)YYLabel *protocolLabel;

@property(nonatomic ,strong)NSString *nickName;

@property(nonatomic ,strong)NSString *avatar;

@property(nonatomic ,strong)NSString *schoolName;

@property(nonatomic ,strong)NSString *gradeName;

@property(nonatomic ,strong)NSString *mobile;

@property(nonatomic ,strong)NSString *username;

@property(nonatomic ,strong)LTClassDetailModel *model;

@end

@implementation LTChangeClassInfoVC
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:LTClearColor] backgroundColor:LTClearColor shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(0, 0, 0, 1),NSFontAttributeName:MFontWithSize(17)} enableScrollEdgeAppearance:NO];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改班级";
    // Do any additional setup after loading the view.
    [self setBackButtonIsHidden:NO];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.saveButton];
    self.view.backgroundColor = rgba(247, 248, 250, 1);
    self.tableView.height = self.titleArray.count *60+10;
    
}
#pragma mark - api

- (void)changeClassInfoWithParame:(NSDictionary *)parame
{
    [LTClassAPIManager changeClassWithParam:parame result:^(NSDictionary * _Nonnull dic) {
        [self backPop];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, (LTScreenWidth - 32)/2.0, 60)];
    textField.userInteractionEnabled = NO;
    textField.font = RFontWithSize(15);
    textField.textColor = LTTextBlack;
    textField.textAlignment = NSTextAlignmentRight;
    [textField setBorderStyle:UITextBorderStyleNone];
    textField.tag = indexPath.row + 1;
    cell.accessoryView = textField;
    if ([self.titleArray[indexPath.row] isEqualToString:@"所属学校"]) {
        textField.text = self.model.schoolname;
    }
    if ([self.titleArray[indexPath.row] isEqualToString:@"班级名字"]) {
        textField.text = self.model.name;
        textField.userInteractionEnabled = YES;
        self.gradeNameTextfield = textField;
    }
    if ([self.titleArray[indexPath.row] isEqualToString:@"所属年级"]) {
        textField.text = self.model.schoolname;
    }
    if ([self.titleArray[indexPath.row] isEqualToString:@"班级人数"]) {
        textField.text = [NSString stringWithFormat:@"%@",self.model.studeentNum];
    }
    if ([self.titleArray[indexPath.row] isEqualToString:@"VIP人数"]) {
        textField.text = [NSString stringWithFormat:@"%@",self.model.vipstudent];
    }
    if ([self.titleArray[indexPath.row] isEqualToString:@"所属学校"]) {
        textField.text = self.model.schoolname;
    }
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.textLabel.font = MFontWithSize(15);
    cell.textLabel.textColor = LTTextBlack;
   
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titleArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return 60;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self endEdit];
}
- (void)saveButtonAction
{
    
    if (self.gradeNameTextfield.text.length > 0) {
        
        [self changeClassInfoWithParame:@{@"id":self.model.class_id,@"name":self.gradeNameTextfield.text}];
    }
}
#pragma mark -private
#pragma mark 打开相机 或者相册


#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(16, LTTopBarHeight + 16, LTScreenWidth - 32, LTScreenHeight - LTTopBarHeight - LTBottomDangerousAreaHeight - 60) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:LTWhiteColor];
        [_tableView setSeparatorColor:LTSeparatorColor];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
       
        _tableView.layer.cornerRadius = 12;
        _tableView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:50/255.0 blue:83/255.0 alpha:0.0500].CGColor;
        _tableView.layer.shadowOffset = CGSizeMake(0,2);
        _tableView.layer.shadowOpacity = 1;
        _tableView.layer.shadowRadius = 12;

    }
    
    return _tableView;
}
- (NSArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = @[@"所属学校",@"班级名字",@"所属年级",@"班级人数",@"VIP人数"];
    }
    return _titleArray;
}
-(UIButton *)saveButton
{
    if (!_saveButton)
    {
        _saveButton = [UIButton new];
        _saveButton.frame =CGRectMake(32, LTScreenHeight - LTBottomDangerousAreaHeight - 60, LTScreenWidth - 64, 44);
       
        [_saveButton setTitle:@"保 存" forState:UIControlStateNormal];
       // [_saveButton setTitleColor:rgba(181, 181, 181, 1) forState:UIControlStateNormal];
        [_saveButton setTitleColor:LTWhiteColor forState:UIControlStateNormal];
        [_saveButton.titleLabel setFont:MFontWithSize(14)];
        [_saveButton addTarget:self action:@selector(saveButtonAction) forControlEvents:UIControlEventTouchUpInside];
        _saveButton.backgroundColor = rgba(41, 121, 255, 1);
        _saveButton.layer.cornerRadius = 22;
        _saveButton.layer.shadowColor = rgba(41, 121, 255, 0.50).CGColor;
        _saveButton.layer.shadowOffset = CGSizeMake(0,4);
        _saveButton.layer.shadowOpacity = 1;
        _saveButton.layer.shadowRadius = 6;
    }
    
    return _saveButton;
}
- (void)setParaObject:(NSObject *)paraObject
{
    self.model = (LTClassDetailModel *)paraObject;
}

@end
